﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using XLZF.MicroServices.Common.Mapping;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Repository;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

#region Ids4

builder.Services.AddAuthentication("Bearer")
    .AddIdentityServerAuthentication(x =>
    {
        x.Authority = "http://localhost:5231";
        x.RequireHttpsMetadata = false;
    });

#endregion

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


#region Autofac

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
    builder.RegisterType<MathRepository>().As<IMathRespository>();  // 直接注册某一个类和接口,左边的是实现类，右边的As是接口

    builder.RegisterType<ClothingClassDictRepository>().As<IClothingClassDictRepository>();

    builder.RegisterType<ClothingClassDetailRepository>().As<IClothingClassDetailRepository>();

    builder.RegisterType<ClothingPicDictRepository>().As<IClothingPicDictRepository>();
});

#endregion

#region 注入
{
    //builder.Services.AddTransient<IMathService, MathService>();
}
#endregion

var configuration = builder.Configuration;

#region JWT

////注册JWTHelper
//builder.Services.AddSingleton(new JwtHelper(configuration));

//// 注册服务
//builder.Services.AddAuthentication(options =>
//{
//    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
//})
//.AddJwtBearer(options =>
//{
//    options.TokenValidationParameters = new TokenValidationParameters()
//    {
//        ValidateIssuer = true, //是否验证Issuer
//        ValidIssuer = configuration["Jwt:Issuer"], //发行人Issuer
//        ValidateAudience = true, //是否验证Audience
//        ValidAudience = configuration["Jwt:Audience"], //订阅人Audience
//        ValidateIssuerSigningKey = true, //是否验证SecurityKey
//        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:SecretKey"])), //SecurityKey
//        ValidateLifetime = true, //是否验证失效时间
//        ClockSkew = TimeSpan.FromSeconds(30), //过期时间容错值，解决服务器端时间不同步问题（秒）
//        RequireExpirationTime = true,
//    };
//});

#endregion

#region Swagger

builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "XLZF.Core", Version = "v1", Description = "E衣橱接口" });

    var file = Path.Combine(AppContext.BaseDirectory, "XLZF.MicroServices.ClothingDictService.xml");//XML绝对路径

    var path = Path.Combine(AppContext.BaseDirectory, file); // xml⽂档绝对路径

    c.IncludeXmlComments(path, true);//true 显示控制器层注释

    c.OrderActionsBy(o => o.RelativePath);//对action的名称进行排序，如果有多个，就可以 看到效果了。
});

#endregion

#region AutoMapper

builder.Services.AddAutoMapper(typeof(AutoMapperConfigs));

#endregion

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthentication();//鉴权
app.UseAuthorization(); //授权

app.MapControllers();

app.Run();
